/*
type int
parse 3 3
parse 3-2-1 0
parse 6-2-1 3
*/

{
  #include <stdio.h>

  struct action { char op; int val; };
  struct action *stack = 0, *sp = 0, *alloc = 0;

  void push(char op, int x) {
    if (sp == alloc) {
      int s = alloc - stack;
      int n = 2 * s + 1;
      stack = realloc(stack, n * sizeof *stack);
      if (!stack) pacc_nomem();
      alloc = stack + n;
      sp = stack + s;
    }
    sp->op = op; sp->val = x;
    ++sp;
  }

  int eval(void) {
    int a;
    while (--sp >= stack) {
      switch(sp->op) {
      case ' ': a = sp->val; break;
      case '-': a -= sp->val; break;
      }
    }
    return a;
  }
}

#Expr ←	d:Digit "-" Expr { push('-', d), 0 }
	#/ d:Digit → { push(' ', d), eval() }


Expr ←	d:Digit ExprTail { push(' ', d), eval() }
ExprTail ← "-" d:Digit ExprTail { push('-', d), 0 }
	/ { 0 }

#ExprTail ← "-" d:Digit e:ExprTail { push(d), e, sub(), 0 }
	#/ { 0 }
#Sub ← d:Digit "-" s:Sub { d - s } / d:Digit → d 
Digit ← [0-9] { ref_0(ref()) - '0' }
